﻿using System;
using System.Collections.Generic;
using System.Data;
using l1cs.server.model.trap;

namespace l1cs.server.datatables
{
    public class TrapTable
    {
        private static Logger _log = Logger.getLogger( typeof( TrapTable ).FullName );

        private static TrapTable _instance;

        private Dictionary<int, L1Trap> _traps = new Dictionary<int, L1Trap>();

        public static TrapTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new TrapTable();
            }
            return _instance;
        }

        private TrapTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM trap";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                foreach ( DataRow item in wrapper.GetDataTable( sqlstr ).Rows )
                {
                    L1Trap trap;
                    String typeName = item["type"] as String;
                    switch ( typeName )
                    {
                        case "L1DamageTrap":
                            trap = new L1DamageTrap( item );
                            break;
                        case "L1HealingTrap":
                            trap = new L1HealingTrap( item );
                            break;
                        case "L1PoisonTrap":
                            trap = new L1PoisonTrap( item );
                            break;
                        case "L1MonsterTrap":
                            trap = new L1MonsterTrap( item );
                            break;
                        case "L1TeleportTrap":
                            trap = new L1TeleportTrap( item );
                            break;
                        case "L1SkillTrap":
                            trap = new L1SkillTrap( item );
                            break;
                        default:
                            continue;
                    }
                    _traps[trap.getId()] = trap;
                }
            }
        }

        public static void reload()
        {/*TTOK*/
            TrapTable oldInstance = _instance;
            _instance = new TrapTable();

            oldInstance._traps.Clear();
        }

        public L1Trap getTemplate( int id )
        {/*TTOK*/
            L1Trap ret;
            _traps.TryGetValue( id, out ret );
            return ret;
        }
    }
}
